{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "56fdbd72",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai # 导入OpenAI，用于调用聊天模型\n",
    "import IPython # 导入IPython，用于展示聊天输出\n",
    "\n",
    "# 这里替换为你的 OpenAI API 密钥\n",
    "openai.api_key  = 'sk-xxxxx'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a01faf5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置OpenAI API调用参数\n",
    "def set_open_params(\n",
    "    model=\"gpt-3.5-turbo\", # 模型\n",
    "    temperature=0.7, # 多样性\n",
    "    n = 1, # 返回结果的数量\n",
    "    max_tokens=256, # 生成结果的最大词数\n",
    "    top_p=1, # 随机抽样的阈值\n",
    "    frequency_penalty=0, # 调整生成的文本的常见度\n",
    "    presence_penalty=0, # 调整生成的文本中的新概念出现的频率\n",
    "):\n",
    "    openai_params = {}    \n",
    "    openai_params['model'] = model  # 使用的模型名称\n",
    "    openai_params['temperature'] = temperature  # 结果多样性的参数，数值越大结果越随机，反之越确定\n",
    "    openai_params['n'] = n  # 返回结果的数量\n",
    "    openai_params['max_tokens'] = max_tokens  # 生成结果的最大词数\n",
    "    openai_params['top_p'] = top_p  # 随机抽样的阈值，较小的值将使结果更加集中，较大的值将使结果更加分散\n",
    "    openai_params['frequency_penalty'] = frequency_penalty  # 调整生成的文本的常见度，较高的值将使模型更可能生成罕见的文本\n",
    "    openai_params['presence_penalty'] = presence_penalty  # 调整生成的文本中的新概念出现的频率，较高的值将使模型更可能引入新的概念\n",
    "    return openai_params\n",
    "\n",
    "params = set_open_params() # 设置参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1675bbde",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从OpenAI API获取完成的文本\n",
    "def get_completion(params, prompt):\n",
    "    messages = [{\"role\": \"user\", \"content\": prompt}]  # 用户角色发送一个消息，消息的内容为我们的提示文本\n",
    "    response = openai.ChatCompletion.create(  # 调用ChatCompletion API\n",
    "        model = params['model'], # 模型\n",
    "        messages=messages, # Prompt消息\n",
    "        temperature = params['temperature'], # 多样性\n",
    "        n = params['n'], # 返回结果的数量\n",
    "        max_tokens = params['max_tokens'], # 生成结果的最大词数\n",
    "        top_p = params['top_p'], # 随机抽样的阈值\n",
    "        frequency_penalty = params['frequency_penalty'], # 生成的文本的常见度\n",
    "        presence_penalty = params['presence_penalty'], # 生成的文本中的新概念出现的频率\n",
    "    )\n",
    "    return response # 接收模型的聊天返回结果"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "be0c3d8c",
   "metadata": {},
   "source": [
    "## Zero-Shot with CoT"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "77cf4e4e",
   "metadata": {},
   "source": [
    "## Zero Shot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f6dc6957",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "```\n",
       "原价：200元\n",
       "打折后价格：200元 * 50% = 100元\n",
       "买3件商品需要支付的钱：100元 * 3 = 300元\n",
       "我需要支付300元。\n",
       "```"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text = \"\"\"I一家商店正在举行打折促销活动。一件商品的原价是200元，现在打折50%。\n",
    "如果我买了3件商品并支付了账单，我需要支付多少钱？\n",
    "\"\"\"\n",
    "\n",
    "prompt = f\"\"\"\n",
    "```{text}```\n",
    "Give the answer of the question delimited by triple backticks.\n",
    "\"\"\"\n",
    "\n",
    "response = get_completion(params, prompt)\n",
    "IPython.display.Markdown(response.choices[0].message[\"content\"])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e4521d3e",
   "metadata": {},
   "source": [
    "## Few Shot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a8eaf7a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<老师>: 当然可以，学习英语就像学习一门新的语言，我们需要注重听、说、读、写四个方面的综合训练。可以通过多听英语音频、多与人交流、多读英语文章、多写英语作文来提高英语能力。同时，记忆常用的单词和短语也是很重要的。"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prompt = f\"\"\"\n",
    "你的任务是用和样例一致的格式回答问题.\n",
    "\n",
    "<学生>: 老师，你能教我学习数学的技巧吗？\n",
    "\n",
    "<老师>: 当然可以，数学就像一个巨大的迷宫，我们需要理清逻辑，逐步探索，最后找到解决问题的线索。\n",
    "\n",
    "<学生>: 老师，你能教我学习语文的技巧吗？\n",
    "\n",
    "<老师>: 当然可以，学习语文就像品味一本好书，需要我们细心研读，领会每个词、每个句子的内涵，透过文字去理解作者的思想和感情。\n",
    "\n",
    "<学生>: 老师，你能教我学习英语的技巧吗？\n",
    "\"\"\"\n",
    "response = get_completion(params, prompt)\n",
    "IPython.display.Markdown(response.choices[0].message[\"content\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7fefef01",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
